package com.rpay.admin.controller.banner;

import com.rpay.admin.param.banner.UpdateBanner;
import com.rpay.admin.param.banner.PostBanner;
import com.rpay.common.dto.banner.BannerDTO;
import com.rpay.common.dto.banner.BannerQueryDTO;
import com.rpay.common.vo.PcsResult;
import com.rpay.service.banner.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author XXX
 * @mail XXX@hotmail.com
 * @date 2018/5/13
 * @Description:后台管理系统Banner图
 */
@RestController
@RequestMapping(value = "api/banner")
@Validated
public class BannerController {
    @Autowired
    private IBannerService iBannerService;

    @GetMapping(value = "")
    public PcsResult list() {
        BannerQueryDTO bannerQueryDTO = null;
        bannerQueryDTO = new BannerQueryDTO();
        bannerQueryDTO.setVisible(null);
        BannerQueryDTO data = iBannerService.list(bannerQueryDTO);
        return PcsResult.build(data);
    }

    @PostMapping(value = "")
    public PcsResult add(@Validated @RequestBody PostBanner postBanner) {
        BannerDTO bannerDTO = new BannerDTO();
        bannerDTO.setUrl(postBanner.getUrl());
        bannerDTO.setEnUrl(postBanner.getEnUrl());
        bannerDTO.setTitle(postBanner.getTitle());
        bannerDTO.setEnTitle(postBanner.getEnTitle());
        bannerDTO.setLinkUrl(postBanner.getLinkUrl());
        bannerDTO.setEnLinkUrl(postBanner.getEnLinkUrl());
        iBannerService.post(bannerDTO);
        return new PcsResult();
    }

    @DeleteMapping(value = "/detail/{id}")
    public PcsResult delete(@PathVariable Integer id) {
        iBannerService.delete(id);
        return new PcsResult();
    }

    @PatchMapping(value = "/detail/{id}")
    public PcsResult patch(@Validated @RequestBody UpdateBanner updateBanner, @PathVariable("id") Integer id) {
        BannerDTO bannerDTO = new BannerDTO();
        bannerDTO.setId(id);
        bannerDTO.setVisible(updateBanner.getVisible());
        bannerDTO.setUrl(updateBanner.getUrl());
        bannerDTO.setEnUrl(updateBanner.getEnUrl());
        bannerDTO.setTitle(updateBanner.getTitle());
        bannerDTO.setEnTitle(updateBanner.getEnTitle());
        bannerDTO.setLinkUrl(updateBanner.getLinkUrl());
        bannerDTO.setEnLinkUrl(updateBanner.getEnLinkUrl());
        iBannerService.patch(bannerDTO);
        return new PcsResult();
    }
}
